﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ActiproSoftware.SyntaxEditor;
using ActiproSoftware.SyntaxEditor.Addons.CSharp;
using ActiproSoftware.SyntaxEditor.Addons.DotNet.Dom;

namespace WindowsUserControl.Codes
{
    /// <summary>
    /// 
    /// </summary>
    public partial class CodeSnippetEditor : UserControl
    {
        private DotNetProjectResolver dotNetProjectResolver = new DotNetProjectResolver();


        /// <summary>
        /// 
        /// </summary>
        public CodeSnippetEditor()
        {
            InitializeComponent();

            SemanticParserService.Start();
            WindowsApplicationContext context = new WindowsApplicationContext();
            // Initialize a .NET project resolver with some typical assemblies 
            WindowsApplicationContext.CacheSplashForm splashForm = new WindowsApplicationContext.CacheSplashForm();
            splashForm.Show();
            splashForm.Refresh();
            dotNetProjectResolver.CachePath = context.ProjectPath + @"Cache\";
            dotNetProjectResolver.AddAllAssembliesInAppDomainAsExternalReferences();
            splashForm.Dispose();

            // Load the language definition
            CSharpSyntaxLanguage language = new CSharpSyntaxLanguage();
            headerEditor.Document.Language = language;
            //headerEditor.Document.Text = headerCode;
            
            footerEditor.Document.Language = language;
            //footerEditor.Document.Text = footerCode;

            fragmentEditor.Document.Filename = "Fragment.cs";
            fragmentEditor.Document.Language = language;
            fragmentEditor.Document.LanguageData = dotNetProjectResolver;
            fragmentEditor.Document.HeaderText = headerEditor.Document.Text;
            fragmentEditor.Document.FooterText = footerEditor.Document.Text;
            fragmentEditor.Document.Reparse();

            headerEditor.Document.TextChanged += new DocumentModificationEventHandler(Document_TextChanged);
            footerEditor.Document.TextChanged += new DocumentModificationEventHandler(Document_TextChanged);
            fragmentEditor.Document.TextChanged +=new DocumentModificationEventHandler(Document_TextChanged);
        }

        void Document_TextChanged(object sender, DocumentModificationEventArgs e)
        {
            fragmentEditor.Document.Reparse();
        }

        private void toggleEditingModeButton_Click(object sender, EventArgs e)
        {
            headerEditor.Document.ReadOnly = !headerEditor.Document.ReadOnly;
            fragmentEditor.Document.ReadOnly = !fragmentEditor.Document.ReadOnly;
            footerEditor.Document.ReadOnly = !footerEditor.Document.ReadOnly;

            if (fragmentEditor.Document.ReadOnly)
            {
                headerEditor.Focus();
            }
            else
            {
                fragmentEditor.Document.HeaderText = headerEditor.Document.Text;
                fragmentEditor.Document.FooterText = footerEditor.Document.Text;
                fragmentEditor.Document.Reparse();
                fragmentEditor.Focus();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string AllCode
        {
            get
            {
                return string.Format("{0}\n{1}\n{2}", this.headerEditor.Document.Text, this.fragmentEditor.Document.Text,
                                     this.footerEditor.Document.Text);
            }

        }

        /// <summary>
        /// 
        /// </summary>
        public string CodeSnippet
        {
            get
            {
                return this.fragmentEditor.Document.Text;
            }
            set
            {
                this.fragmentEditor.Document.Text = value;
                this.fragmentEditor.Document.Reparse();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string ExternalAssembly
        {
            set
            {
                this.dotNetProjectResolver.AddExternalReference(value);
            }
        }
        /// <summary></summary>
        public string HeaderCode
        {
            get
            {
                return this.headerEditor.Document.Text;
            }
            set
            {
                this.headerEditor.Document.Text = value;
                this.fragmentEditor.Document.Reparse();
            }
        }
        /// <summary></summary>
        public string FooterCode
        {
            get
            {
                return this.footerEditor.Document.Text;
            }
            set
            {
                this.footerEditor.Document.Text = value;
                this.fragmentEditor.Document.Reparse();
            }
        }
    }
}
